package xyz.quanguanjia.masterboy;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

public class MBDUploadGroupRecycleAdapter extends RecyclerView.Adapter {
    private List<MBDUploadGroupModel> uploadGroups;

    private MBDGroupDetailViewHolder.ClickListener mListener;

    public MBDUploadGroupRecycleAdapter(List<MBDUploadGroupModel> gd) {
        this.uploadGroups = gd;
    }

    public void clickListener(MBDGroupDetailViewHolder.ClickListener listener){
        this.mListener = listener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        switch (viewType) {
            case MBDUploadGroupModel.TYPE_GROUP_CONFIRM_BUTTON: {
                // 创建列表项视图
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_cell_upload_group_confirm, parent, false);
                MBDGroupDetailViewHolder holder = new MBDGroupDetailViewHolder(view);
                holder.setListener(this.mListener);
                return  holder;
            }

            case MBDUploadGroupModel.TYPE_GROUP_TYPE:
            case MBDUploadGroupModel.TYPE_GROUP_DESC:
            case MBDUploadGroupModel.TYPE_GROUP_ADMIN:
            case MBDUploadGroupModel.TYPE_GROUP_NAME: {
                // 创建列表项视图
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_cell_upload_group_title_detail, parent, false);
                return new MBDGroupDetailViewHolder(view);
            }
            case MBDUploadGroupModel.TYPE_GROUP_CODE:
            case MBDUploadGroupModel.TYPE_GROUP_LOGO: {
                // 创建列表项视图
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_cell_upload_group_title_pic, parent, false);
                return new MBDGroupDetailViewHolder(view);
            }
            default:
                break;
        }
        // 创建列表项视图
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_cell_upload_group, parent, false);
        return new MBDGroupDetailViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        // 绑定数据到列表项视图
        MBDUploadGroupModel detail = this.uploadGroups.get(position);
        ((MBDGroupDetailViewHolder) holder).bind(detail);
    }

    @Override
    public int getItemCount() {
        return this.uploadGroups.size();
    }

    @Override
    public int getItemViewType(int position) {
        return this.uploadGroups.get(position).type;
    }

    public static class MBDGroupDetailViewHolder extends RecyclerView.ViewHolder {
        private ClickListener mListener;

        private TextView nameTextView;
        private TextView detailTextView;
        private Button uploadGroupConfirmButton;
        private ImageView uploadAddImageView;

        public MBDGroupDetailViewHolder(@NonNull View itemView) {
            super(itemView);
            nameTextView = itemView.findViewById(R.id.tv_upload_group_title);
            detailTextView = itemView.findViewById(R.id.tv_upload_group_detail);
            uploadGroupConfirmButton = itemView.findViewById(R.id.btn_uplod_group_confirm);
            uploadAddImageView = itemView.findViewById(R.id.img_upload_add);

        }

        public void setListener(ClickListener listener){
            this.mListener = listener;
        }

        public void bind(MBDUploadGroupModel detail) {
            // 设置列表项视图的数据
            if (null != nameTextView) {
                nameTextView.setText(detail.name);
            }

            if (null != detailTextView) {
                detailTextView.setText(detail.detail);
            }

            if (null != uploadAddImageView) {
//                uploadAddImageView.setText(detail.detail);
                boolean emptyDetail = TextUtils.isEmpty(detail.detail) ;
                boolean emptyCode = (detail.type == MBDUploadGroupModel.TYPE_GROUP_CODE) && detail.detail.equals(MBDUploadGroupModel.EMPTY_GROUP_CODE) ;
                boolean emptyLogo = (detail.type == MBDUploadGroupModel.TYPE_GROUP_LOGO) && detail.detail.equals(MBDUploadGroupModel.EMPTY_GROUP_LOGO) ;
                boolean empty = emptyDetail || emptyCode || emptyLogo;

                if (!empty){
                    String imageUrl = MBDPath2URLUtils.wrapAvatarURL(detail.detail);

                    int cornerRadius = 10; // 圆角半径，单位为像素
                    RequestOptions options = new RequestOptions()
                            .transform(new CenterCrop(),new RoundedCorners(cornerRadius));

                    Glide.with(MBDApplication.getContext())
                            .load(imageUrl)
                            .apply(options)
                            .into(uploadAddImageView);
                }


            }

            if (null != uploadGroupConfirmButton) {
                uploadGroupConfirmButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (null != MBDGroupDetailViewHolder.this.mListener) {
                            MBDGroupDetailViewHolder.this.mListener.confirmButtonClicked();
                        }
                    }
                });
            }


//            String groupD = detail.groupDesc.replace("\n", " ");;
//            ageTextView.setText(groupD);
//
//            String imageUrl = MBDHostManager.IMAGE_HOST + detail.groupLogo;
//
//            int cornerRadius = 10; // 圆角半径，单位为像素
//            RequestOptions options = new RequestOptions()
//                    .transform(new CenterCrop(),new RoundedCorners(cornerRadius));
//
//            Glide.with(MBDApplication.getContext())
//                    .load(imageUrl)
//                    .apply(options)
//                    .into(groupLogo);

        }

        public interface ClickListener {
            public void confirmButtonClicked();
        }
    }


}
